import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.openroom.editing.JPEGImageProcessor;

public class RotateImageJava {

	private static BufferedImage original;

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		original = ImageIO.read(new File("/home/nitish/3.jpg"));
		long start = System.nanoTime();
		rotateUsingJava();
		long end = System.nanoTime();
		System.out.println("RotateImageJava.main() using Java finished in "
				+ (end - start) / 1000000 + " ms");
		start = end;
		JPEGImageProcessor ip2 = JPEGImageProcessor
				.createImageProcessor(original);
		ip2 = ip2.rotateLeft();
		BufferedImage createImage = ip2.createImage();
		ImageIO.write(createImage, "jpg", new File("/home/nitish/IMG_9496_3.JPG"));
		end = System.nanoTime();
		System.out.println("RotateImageJava.main() using ImageJ finished in " + (end - start)
				/ 1000000 + " ms");
	}

	private static void rotateUsingJava() throws IOException {

		AffineTransform trans = new AffineTransform();
		trans.translate(original.getWidth() / 2, original.getHeight() / 2);
		trans.rotate(Math.toRadians(90));
		int diff = (original.getWidth() - original.getHeight()) / 2;
		trans.translate(-2 * diff, -diff);
		BufferedImage dest = new BufferedImage(original.getHeight(),
				original.getWidth(), original.getType());
		Graphics2D g = (Graphics2D) dest.getGraphics();
		AffineTransformOp aop = new AffineTransformOp(trans,
				AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		BufferedImage filter = aop.filter(original, dest);
		ImageIO.write(filter, "jpg", new File("/home/nitish/IMG_9496_2.JPG"));
	}

}
